package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.BaseAttribute;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;


    @Autowired
    private BaseCategoryViewMapper baseCategoryViewMapper;

    @Autowired
    private BaseAttributeMapper baseAttributeMapper;

    /**
     * 获取所有分类的三级分类列表
     * @return
     */
    @Override
    public List<JSONObject> getBaseCategoryList() {

        //先拿一级分类列表  需要拿全部数据  遍历取出来
        List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(null);
        Map<Long, List<BaseCategoryView>> categoryViewMap = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = categoryViewMap.entrySet().iterator();
        List<JSONObject> list = new ArrayList<>();
        while (iterator.hasNext()){
            //获取元素用
            Map.Entry<Long, List<BaseCategoryView>> categoryEntry = iterator.next();
            Long category1Id = categoryEntry.getKey();
            String category1Name = categoryEntry.getValue().get(0).getCategory1Name();

            JSONObject category1 = new JSONObject();

            category1.put("categoryId",category1Id);
            category1.put("categoryName",category1Name);

            //在这个所有数据大Map里再找出  二级分类的数据

            Map<Long, List<BaseCategoryView>> category2Map = categoryEntry.getValue().stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator1 = category2Map.entrySet().iterator();
            ArrayList<JSONObject> category2Child = new ArrayList<>();

            while (iterator1.hasNext()){
                 Map.Entry<Long, List<BaseCategoryView>> category2Entry = iterator1.next();
                 Long category2Id = category2Entry.getKey();
                 String category2Name = category2Entry.getValue().get(0).getCategory2Name();

                 //构建二级对象

                 JSONObject category2= new JSONObject();
                 category2.put("categoryId",category2Id);
                 category2.put("categoryName",category2Name);


                 List<JSONObject> category3Child = category2Entry.getValue().stream().map(baseCategoryView -> {

                     JSONObject category3 = new JSONObject();
                     category3.put("categoryId", baseCategoryView.getCategory3Id());
                     category3.put("categoryName", baseCategoryView.getCategory3Name());
                     return category3;
                 }).collect(Collectors.toList());
                 category2.put("categoryChild",category3Child);
                 category2Child.add(category2);

             }
             category1.put("categoryChild",category2Child);
              list.add(category1);
        }
        return list;
    }

    /**
     * 根据一级分类获取三级分类下全部数据
     * @param category1Id
     * @return
     */
    @Override
    public JSONObject getBaseCategoryList(Long category1Id) {
        //把第一个对象id和name赋值
        JSONObject category1 = new JSONObject();
        category1.put("categoryId",category1Id);
        List<BaseCategoryView> categoryViewList = baseCategoryViewMapper.selectList
                (new LambdaQueryWrapper<BaseCategoryView>().eq(BaseCategoryView::getCategory1Id, category1Id));
        category1.put("category1Name",categoryViewList.get(0).getCategory1Name());
        //根据1级分类的集合 父找子
        Map<Long, List<BaseCategoryView>> listMap = categoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
        Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = listMap.entrySet().stream().iterator();
        ArrayList<JSONObject> category2ChildList = new ArrayList<>();
        while (iterator.hasNext()){
            Map.Entry<Long, List<BaseCategoryView>> category2Entry = iterator.next();
            Long category2Id = category2Entry.getKey();
            String category2Name = category2Entry.getValue().get(0).getCategory2Name();
            JSONObject category2 = new JSONObject();
            category2.put("categoryId",category2Id);
            category2.put("categoryName",category2Name);
            List<BaseCategoryView> category3Value = category2Entry.getValue();
            category3Value.stream().map(baseCategoryView->{
                JSONObject category3 = new JSONObject();
                category3.put("categoryId", baseCategoryView.getCategory3Id());
                category3.put("categoryName", baseCategoryView.getCategory3Name());
                return category3;
            }).collect(Collectors.toList());
            category2.put("categoryChild",category3Value);
            category2ChildList.add(category2);

        }
        category1.put("categoryChild",category2ChildList);
        return category1;
    }
    /**
     * 根据分类id获取分类信息
     *
     * @param category3Id
     * @return
     */
    @Override
    public BaseCategoryView getBaseCategoryViewByCategoryId(Long category3Id) {

        return baseCategoryViewMapper.selectById(category3Id);
    }
}

